package com.topisv.tms.utils.rfd;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.topisv.tms.entity.EdiGoodsSku;
import com.topisv.tms.entity.EdiOrder;
import com.topisv.tms.entity.WarehouseAddress;
import com.topisv.tms.service.vo.invoice.MDParam;
import com.topisv.tms.utils.SystemUtil;
import com.topisv.tms.utils.rfd.vo.OrderDetial;
import com.topisv.tms.utils.rfd.vo.OrderModel;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class MailRFDUtil {
	
	private static final Logger log = LoggerFactory.getLogger(MailRFDUtil.class);
	
	private static String Init = "pyyc001";
	private static String rfd_url = "http://219.141.203.180:6001/api/order/Import";
	private static String key = "datrg2eimGPmkWh4VEkngSaA9tgpyyc+";
	private static String Identity = "pyyc6659-5e76-ae3e-f927-3f20b950000";
	/*private static Map<String, String> mouthMaps=new HashMap<String, String>();
	static{
		try {
			Properties p=InitProperties.initWeService();
		    String params=p.getProperty("sfParams");
		    if(params!=null){
		    	Init=params;
		    }
		    String sfMonth=p.getProperty("sfMonth");
		    if(sfMonth!=null){
		    	String[] lists=sfMonth.split(",");
		    	for(String list:lists){
		    		String[] ls=list.split(":");
		    		mouthMaps.put(ls[0], ls[1]);
		    	}
		    }
		} catch (IOException e) {
			// TODO Auto-generated catch block
			
		}
		
	}
	
	public static String getKey(String key){
		return mouthMaps.get(key);
	}*/
	
	public static String  getRFDMailNo(EdiOrder e,List<EdiGoodsSku> skuList,WarehouseAddress warehouseAddress){
		 String json=createOrdeno(e,skuList,warehouseAddress);
		 log.info("申请如风达运单号：" + json);
		 String token = SystemUtil.MD5(json + "|" + key);
		 log.info("申请如风达运单号：" + (MDParam.RFD_URL+"/api/order/Import?identity="+ Identity + "&token=" + token));
		 String result = SystemUtil.pub(MDParam.RFD_URL+"/api/order/Import?identity="+ Identity + "&token=" + token, json);
		 log.info("申请如风达运单号结果：" + result);
	    return result;
	}

	//创建订单
	public static String createOrdeno(EdiOrder e,List<EdiGoodsSku> skuList,WarehouseAddress warehouseAddress){
		List<OrderModel> orderModels = new ArrayList<OrderModel>();
		OrderModel orderModel = new OrderModel();
		orderModel.setMerchantCode(Init);
		orderModel.setFormCode(e.getOrdeno());
		orderModel.setPerFormCode("");
		orderModel.setTotalAmount(200f);
		orderModel.setPaidAmount(200f);
		orderModel.setReceiveAmount(0f);
		orderModel.setRefundAmount(0f);
		orderModel.setInsureAmount(0f);
		orderModel.setFromName(warehouseAddress.getContact());
		orderModel.setFromAddress(warehouseAddress.getAddress());
		orderModel.setFromProvince(warehouseAddress.getProvName());
		orderModel.setFromCity(warehouseAddress.getCityName());
		orderModel.setFromArea(warehouseAddress.getDistName());
		orderModel.setFromPostCode("");
		orderModel.setFromMobile(warehouseAddress.getMobile());
		orderModel.setFromPhone(warehouseAddress.getMobile());
		orderModel.setCashType("1");
		orderModel.setToName(e.getRecena());
		orderModel.setToAddress(e.getAddres());
		orderModel.setToProvince(e.getProvna());
		orderModel.setToCity(e.getCityna());
		orderModel.setToArea(e.getDistna());
		orderModel.setToPostCode("");
		orderModel.setToMobile(e.getMobile());
		orderModel.setToPhone(e.getTeleph());
		orderModel.setComment("");
		orderModel.setExtendData("");
		orderModel.setWaybillNo("0");
		orderModel.setOrderType("0");
		orderModel.setDeliverCode("");
		orderModel.setDistributionCode("");
		orderModel.setCurrentDistributioncode("");
		orderModel.setSortingCenterId("");
		orderModel.setSortingCenterName(warehouseAddress.getSortingCenterName());
		orderModel.setWareHouseId("");
		orderModel.setWarehouseName("");
		
		float weight = 0;
		String sku_id = "";
		String skuName="";
		String unit = "个";
		if("MID".equals(e.getCustomerCode()))
		{
			if("single".equals(e.getOrderType()))
			{
				skuName = skuList.get(0).getSkuName();
				weight = (skuList.get(0).getWeight() + skuList.get(0).getWeightPkg())/1000;
				unit = skuList.get(0).getUnit();
				sku_id = skuList.get(0).getSku();
			}
			else
			{
				int _weight=0;
				for(EdiGoodsSku sku : skuList)
				{
					if("1".equals(sku.getSellFlg()))
					{
						skuName = sku.getSkuName();
						unit=sku.getUnit();
						sku_id = sku.getSku();
					}
					_weight += sku.getWeight()+sku.getWeightPkg();
				}
				weight = _weight/1000;
			}
		}
		else
		{
			if("single".equals(e.getOrderType()))
			{
				weight = (skuList.get(0).getWeight() + skuList.get(0).getWeightPkg())/1000;
				unit = skuList.get(0).getUnit();
				skuName = skuList.get(0).getSkuName();
				sku_id = skuList.get(0).getSku();
			}
			else
			{
				int _weight=0;
				for(EdiGoodsSku sku : skuList)
				{
					_weight += sku.getWeight()+sku.getWeightPkg();
					skuName = sku.getSkuName();
					sku_id = sku.getSku();
				}
				weight = _weight/1000;
			}
		}
		orderModel.setWeight(weight);
        List<OrderDetial> orderDetials = new ArrayList<OrderDetial>();
        OrderDetial orderDetial = new OrderDetial();
        orderDetial.setProductName(skuName);
        orderDetial.setProductCode(sku_id);
        orderDetial.setSellPrice("0");
        orderDetial.setCount("1");
        orderDetial.setSize("");
        orderDetial.setUnit(unit);
        orderDetials.add(orderDetial);
        orderModel.setOrderDetials(orderDetials);
        orderModels.add(orderModel);
        JSONArray ja = JSONArray.fromObject(orderModels);
		return ja.toString();
	}
	
	//取消订单
	public static String confirmOrder(String ordeno,String waybillno){
		JSONObject json = new JSONObject();
		json.put("formCode", ordeno);
		json.put("waybillNo", waybillno);
		json.put("packageCount", 1);
		return "["+ json.toString() + "]";
	}
	
	//取消订单
		public static String routeinfo(){
			JSONObject json = new JSONObject();
			json.put("OrderType", "0");
			json.put("Orders", "");
			json.put("PackageCount", 1);
			json.put("Weight", 15.21);
			return "["+ json.toString() + "]";
		}
	
	public static void main(String[] args)
	{
		
		String url = "http://edi.wuliusys.com/api/order/OutStore";
		//String url = "http://219.141.203.180:6001/api/order/Import";
		String key = "datrg2eimGPmkWh4VEkngSaA9tgpyyc+";
		String Identity = "pyyc6659-5e76-ae3e-f927-3f20b950000";
		String json = "[{\"waybillNo\":\"11508067688324\",\"packageCount\":1,\"formCode\":\"PD143884762068549\"}]";
		String token = SystemUtil.MD5(json + "|" + MDParam.RFD_KEY);
		log.info("如风达出库确认:"+"/api/order/OutStore?identity="+ MDParam.RFD_Identity + "&token=" + token);
		String result = SystemUtil.pub("http://edi.wuliusys.com/api/order/OutStore?identity="+ MDParam.RFD_Identity + "&token=" + token, json);
		//String token = SystemUtil.MD5(json + "|" + key);
		System.out.println("json =====" + json);
		System.out.println("token ===== " +token);
		System.out.println(result);
	}
   
}
